#include "boxgroup.h"
#include "onebox.h"
#include <QDateTime>
#include <QDebug>
#include <QKeyEvent>
#include <QTimer>
#include <QGraphicsScene>
#include <QPainter>
BoxGroup::BoxGroup(QGraphicsItem *parent, BoxShape shape)
    : QGraphicsItemGroup(parent), currentShape(shape)
{
    randomGenerator = new QRandomGenerator(QDateTime::currentDateTime().toSecsSinceEpoch());
    initBoxGroup(shape);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &BoxGroup::moveOneStep);
}

QRectF BoxGroup::boundingRect() const
{
    qreal penWidth = 1;

    return QRectF(-40 - penWidth / 2, -40 - penWidth / 2,
                  80 + penWidth, 80 + penWidth);
}

void BoxGroup::keyPressEvent(QKeyEvent *event)
{
    //判断是否开始游戏
    if(!gameFlag) return;
    //加一个判断条件，避免无内容的下移(否则重按空格时会陷入死循环)
    if(members->isEmpty()) return;
    //up: 变形(变形方式为顺时针旋转90度)
    if(event->key() == Qt::Key_Up){
        setRotation(rotation() + 90);
        if(isColliding()){
            setRotation(rotation() - 90);
        }
    }
    //down: 下落
    else if(event->key() == Qt::Key_Down){
        moveBy(0, 20);
        if(isColliding()){
            moveBy(0, -20);
            moveBoxToScene();
            emit needNewBox();
        }
    }

    //space: 连续下落
    else if(event->key() == Qt::Key_Space){
        moveBy(0, 20);
        while(!isColliding()){
            moveBy(0, 20);
        }
        moveBy(0, -20);
        moveBoxToScene();
        emit needNewBox();
    }

    //left: 左移一格
    else if(event->key() == Qt::Key_Left){
        moveBy(-20, 0);
        if(isColliding()){
            moveBy(20, 0);
        }
    }

    //right: 右移一格
    else if(event->key() == Qt::Key_Right){
        moveBy(20, 0);
        if(isColliding()){
            moveBy(-20, 0);
        }
    }
}

bool BoxGroup::isColliding()
{
    foreach (OneBox *box, *members) {
        if(box->collidingItems().size() > 0) {
//            qDebug() << "碰撞的原模块位置："<<box->pos();
//            QList<QGraphicsItem *> list = box->collidingItems();
//            for(int i = 0; i < list.size(); i++){
//                OneBox *temp = new OneBox(this, Qt::blue);
//                this->scene()->addItem(temp);
//                qDebug() << "碰撞的当前模块位置："<<list.at(i)->pos();
//                temp->setPos(list.at(i)->pos());
//            }
            return true;
        }
    }
    return false;
}

void BoxGroup::setGameFlag(bool gameFlag)
{
    this->gameFlag = gameFlag;
}

void BoxGroup::initBoxGroup(BoxShape shape)
{
    static const QColor colorTable[7] = {
        QColor(200, 0, 0, 100), QColor(255, 200, 0, 100),
        QColor(0, 0, 200, 100), QColor(0, 200, 0, 100),
        QColor(0, 200, 255, 100), QColor(200, 0, 255, 100),
        QColor(150, 100, 100, 100)
    };
    setPos(500, 70);
    members = new QList<OneBox*>();
    if(shape == RandomShape){
        int high = ZShape;
        shape = static_cast<BoxShape>(randomGenerator->bounded(high));
    }
    int index = shape;
    for(int i = 0; i < 4; i++){
        OneBox *box = new OneBox(this, colorTable[index]);
        box->setGroup(this);
        members->append(box);
        box->setPos(boxPosGroup[index][i][0], boxPosGroup[index][i][1]);
    }
}

void BoxGroup::moveOneStep()
{
    if(!gameFlag) return;
    moveBy(0, 20);
    if(isColliding()){
        moveBy(0, -20);
        moveBoxToScene();
        emit needNewBox();
    }
}

void BoxGroup::moveBoxToScene()
{
    foreach (OneBox *box, *members) {
        removeFromGroup(box);
        QGraphicsScene *scene = this->scene();
        scene->addItem(box);
    }
    members->clear();
    endTimer();
}

QPainterPath BoxGroup::shape() const
{
    QPainterPath path;
    path.addRect(0, 0, 0, 0);
    return path;
}

void BoxGroup::startTimer(int msec)
{
    timer->start(msec);
    gameFlag = true;
}

void BoxGroup::endTimer()
{
    timer->stop();
    gameFlag = false;
}

void BoxGroup::moveToScreen()
{
    setPos(300, 70);
}

void BoxGroup::clearBox()
{
    foreach (OneBox *box, *members) {
        box->deleteLater();
    }
    members->clear();
}


